{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "'''\n",
    "折线图  plot\n",
    "条形图  bar\n",
    "饼图 pie\n",
    "散点图  scatter\n",
    "箱线图  boxplot\n",
    "多个图表\n",
    "组合图\n",
    "'''"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 绘制折线图\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import rcParams # 字体\n",
    "# rcParams['font.family'] = 'STHeiti' #mac  win:SimHei\n",
    "rcParams['font.sans-serif'] = 'STHeiti'\n",
    "# 创建图表，设置大小\n",
    "plt.figure(figsize=(10,5))\n",
    "\n",
    "# 要绘图的数据\n",
    "month = ['1月','2月','3月','4月']\n",
    "sales = [100,150,80,130]\n",
    "# 绘制折线图\n",
    "plt.plot(month,sales,\n",
    "         label='产品A',\n",
    "         color='orange',\n",
    "         linewidth=2,\n",
    "         linestyle='--',\n",
    "         marker='o',)\n",
    "\n",
    "# 添加标题\n",
    "plt.title('2025年销售趋势', color='red',fontsize=20)\n",
    "# 添加坐标轴的标签\n",
    "plt.xlabel('月份',fontsize=10)\n",
    "plt.ylabel('销售额（万元）',fontsize=10)\n",
    "# 添加图例\n",
    "plt.legend(loc='upper left')\n",
    "# 添加网格线\n",
    "plt.grid(True,alpha=0.1,color='blue',linestyle='--')\n",
    "# plt.grid(axis='x')\n",
    "# 设置刻度字体大小\n",
    "plt.xticks(rotation=0,fontsize=12)\n",
    "plt.yticks(rotation=0,fontsize=12)\n",
    "# 设置y轴的范围\n",
    "plt.ylim(0,160)\n",
    "# 在每个数据点上显示数值\n",
    "for x,y in zip(month,sales):\n",
    "    plt.text(x,y+1,str(y),ha='center',va='bottom',fontsize=10)\n",
    "# 显示图表\n",
    "plt.show()"
   ],
   "id": "a3fdce094b37e108",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 绘制柱状图\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import rcParams # 字体\n",
    "# rcParams['font.family'] = 'STHeiti' #mac  win:SimHei\n",
    "rcParams['font.sans-serif'] = 'STHeiti'\n",
    "# 创建图表，设置大小\n",
    "plt.figure(figsize=(10,5))\n",
    "\n",
    "# 要绘图的数据\n",
    "subjects = ['语文','数学','英语','科学']\n",
    "scores = [85, 92, 78, 88]\n",
    "\n",
    "# 绘制柱状图\n",
    "plt.bar(subjects,scores,\n",
    "        label='小红',\n",
    "        color='orange',\n",
    "        width=0.6,)\n",
    "#\n",
    "# 添加标题\n",
    "plt.title('2025年成绩分布', color='red',fontsize=20)\n",
    "# 添加坐标轴的标签\n",
    "plt.xlabel('科目',fontsize=10)\n",
    "plt.ylabel('分数',fontsize=10)\n",
    "# 添加图例\n",
    "plt.legend(loc='upper right')\n",
    "# 添加网格线\n",
    "plt.grid(axis='y',alpha=0.1,color='blue',linestyle='--')\n",
    "# plt.grid(axis='x')\n",
    "# 设置刻度字体大小\n",
    "plt.xticks(rotation=0,fontsize=12)\n",
    "plt.yticks(rotation=0,fontsize=12)\n",
    "# 设置y轴的范围\n",
    "plt.ylim(0,100)\n",
    "# 在每个数据点上显示数值\n",
    "for x,y in zip(subjects,scores):\n",
    "    plt.text(x,y+1,str(y),ha='center',va='bottom',fontsize=10)\n",
    "# 自动优化排版\n",
    "plt.tight_layout()\n",
    "# 显示图表\n",
    "plt.show()"
   ],
   "id": "8fe4a9863096958",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 绘制条形图\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import rcParams # 字体\n",
    "# rcParams['font.family'] = 'STHeiti' #mac  win:SimHei\n",
    "rcParams['font.sans-serif'] = 'STHeiti'\n",
    "# 创建图表，设置大小\n",
    "plt.figure(figsize=(10,5))\n",
    "\n",
    "# 要绘图的数据\n",
    "countries = ['United States','China','Japan','Germany','India']\n",
    "gdp = [ 92,78, 43, 22,8]\n",
    "#\n",
    "# 绘制条形图\n",
    "plt.barh(countries,gdp,color='orange',)\n",
    "\n",
    "# 添加标题\n",
    "plt.title('2025年GDP排名', color='red',fontsize=20)\n",
    "# 添加坐标轴的标签\n",
    "plt.xlabel('GDP',fontsize=10)\n",
    "plt.ylabel('国家',fontsize=10)\n",
    "# 自动优化排版\n",
    "plt.tight_layout()\n",
    "# 显示图表\n",
    "plt.show()"
   ],
   "id": "3d07bfc4d866ac54",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 绘制饼图\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import rcParams # 字体\n",
    "# rcParams['font.family'] = 'STHeiti' #mac  win:SimHei\n",
    "rcParams['font.sans-serif'] = 'STHeiti'\n",
    "# 创建图表，设置大小\n",
    "plt.figure(figsize=(10,5))\n",
    "# 要绘图的数据\n",
    "things = ['学习','娱乐','运动','睡觉','其他']\n",
    "times = [6,4,1,8,5]\n",
    "colors = ['#66b3ff','#99ff99','#ffcc99','#ff9999','#ff4499']  #配色\n",
    "# 绘制饼图\n",
    "plt.pie(times,labels=things,\n",
    "        autopct='%.1f%%',  # 显示百分比\n",
    "        startangle=90, #调整初始画图的角度\n",
    "        colors = colors,\n",
    "        )\n",
    "# 添加标题\n",
    "plt.title('一天的时间分布', color='red',fontsize=20)\n",
    "# 自动优化排版\n",
    "plt.tight_layout()\n",
    "# 显示图表\n",
    "plt.show()"
   ],
   "id": "8984c054988c1400",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 绘制环形图\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import rcParams # 字体\n",
    "# rcParams['font.family'] = 'STHeiti' #mac  win:SimHei\n",
    "rcParams['font.sans-serif'] = 'STHeiti'\n",
    "# 创建图表，设置大小\n",
    "plt.figure(figsize=(10,5))\n",
    "# 要绘图的数据\n",
    "things = ['学习','娱乐','运动','睡觉','其他']\n",
    "times = [6,4,1,8,5]\n",
    "colors = ['#66b3ff','#99ff99','#ffcc99','#ff9999','#ff4499']  #配色\n",
    "# 绘制饼图\n",
    "plt.pie(times,labels=things,\n",
    "        autopct='%.1f%%',  # 显示百分比\n",
    "        startangle=90, #调整初始画图的角度\n",
    "        colors = colors,# 设置饼图的配色\n",
    "        wedgeprops={'width':0.6},#设置圆环的宽度\n",
    "        pctdistance=0.6, #设置百分比的位置\n",
    "        )\n",
    "# 添加标题\n",
    "plt.title('一天的时间分布', color='red',fontsize=20)\n",
    "plt.text(0,0,'总计：\\n100%',ha='center',va='bottom',fontsize=10)\n",
    "# 自动优化排版\n",
    "plt.tight_layout()\n",
    "# 显示图表\n",
    "plt.show()"
   ],
   "id": "19b7e618c88ea10a",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 绘制爆炸式饼图\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import rcParams # 字体\n",
    "# rcParams['font.family'] = 'STHeiti' #mac  win:SimHei\n",
    "rcParams['font.sans-serif'] = 'STHeiti'\n",
    "# 创建图表，设置大小\n",
    "plt.figure(figsize=(10,5))\n",
    "# 要绘图的数据\n",
    "things = ['学习','娱乐','运动','睡觉','其他']\n",
    "times = [6,4,1,8,5]\n",
    "colors = ['#66b3ff','#99ff99','#ffcc99','#ff9999','#ff4499']  #配色\n",
    "explode = [0.1,0,0,0,0]#设置突出块的位置\n",
    "# 绘制饼图\n",
    "plt.pie(times,labels=things,\n",
    "        autopct='%.1f%%',  # 显示百分比\n",
    "        startangle=0, #调整初始画图的角度\n",
    "        colors = colors,\n",
    "        explode = explode,#设置突出块\n",
    "        shadow=True,\n",
    "        )\n",
    "# 添加标题\n",
    "plt.title('一天的时间分布', color='red',fontsize=20)\n",
    "# 自动优化排版\n",
    "plt.tight_layout()\n",
    "# 显示图表\n",
    "plt.show()"
   ],
   "id": "200bc085d9ba36b9",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 绘制散点图\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import rcParams # 字体\n",
    "# rcParams['font.family'] = 'STHeiti' #mac  win:SimHei\n",
    "rcParams['font.sans-serif'] = 'STHeiti'\n",
    "# 创建图表，设置大小\n",
    "plt.figure(figsize=(10,5))\n",
    "\n",
    "# 要绘图的数据\n",
    "scores=[50,55,60,65,70,75,80]\n",
    "hours=[1,2,3,4,5,6,7]\n",
    "\n",
    "# 绘制散点图\n",
    "plt.scatter(hours,scores)\n",
    "\n",
    "# 显示图表\n",
    "plt.show()"
   ],
   "id": "ec075242ad7e9ef3",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 箱线图\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 模拟 3 门课的成绩\n",
    "data = {\n",
    "    '语文': [82, 85, 88, 70, 90, 76, 84, 83, 95],\n",
    "    '数学': [75, 80, 79, 93, 88, 82, 87, 89, 92],\n",
    "    '英语': [70, 72, 68, 65, 78, 80, 85, 90, 95]\n",
    "}\n",
    "plt.figure(figsize=(8, 6))\n",
    "plt.boxplot(data.values(), tick_labels=data.keys())\n",
    "\n",
    "plt.title(\"各科成绩分布（箱线图）\")\n",
    "plt.ylabel(\"分数\")\n",
    "plt.grid(True, axis='y', linestyle='--', alpha=0.5)\n",
    "plt.show()"
   ],
   "id": "e053a14c59aecee",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "'''\n",
    "折线图：趋势随时间变化\n",
    "条形图/柱状图：类别之间对比\n",
    "饼图：整体组成比例\n",
    "散点图：两变量相关性\n",
    "箱线图：数据分布、异常\n",
    "'''"
   ],
   "id": "c9c5c208b9e1c127",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 多个图的绘制方法\n",
    "import matplotlib.pyplot as plt\n",
    "# 要绘图的数据\n",
    "month = ['1','2','3','4']\n",
    "sales = [100,150,80,130]\n",
    "\n",
    "# 动态图表的生成\n",
    "# f1 = plt.subplot(2,2,1) #生成一个子图 行 列 索引\n",
    "f1 = plt.subplot(221) #生成一个子图 行 列 索引\n",
    "f1.plot(month,sales)\n",
    "f2 = plt.subplot(2,2,2)\n",
    "f2.bar(month,sales)\n",
    "f3 = plt.subplot(2,2,3)\n",
    "f3.scatter(month,sales)\n",
    "f4 = plt.subplot(224)\n",
    "f4.barh(month,sales)"
   ],
   "id": "f8f2401f5dcc084d",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "# 分析案例：温度分析\n",
    "# 1. 导入库\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import rcParams\n",
    "rcParams['font.sans-serif'] = 'STHeiti'\n",
    "\n",
    "# 2. 导入数据\n",
    "df = pd.read_csv('data/weather.csv')\n",
    "df.head()\n",
    "# 绘制气温的趋势变化图\n",
    "df['date'] = pd.to_datetime(df['date'])\n",
    "df=df[df['date'].dt.year==2015]\n",
    "plt.figure(figsize=(15,10))\n",
    "plt.plot(df['date'],df['temp_max'],label='最高气温')\n",
    "plt.plot(df['date'],df['temp_min'],label='最低气温')\n",
    "plt.title('2015年气温趋势变化图')\n",
    "plt.xlabel('日期')\n",
    "plt.ylabel('气温')\n",
    "plt.legend()\n",
    "\n",
    "# 绘制降水量的直方图\n",
    "df['temp_mean'] = (df['temp_max'] + df['temp_min'])/2\n",
    "plt.figure(figsize=(15,10))\n",
    "plt.plot(df['date'],df['temp_mean'],label='平均气温')\n",
    "plt.title('2015年气温趋势变化图')\n",
    "plt.xlabel('日期')\n",
    "plt.ylabel('气温')\n",
    "plt.legend()\n",
    "# 绘制降水量的直方图\n",
    "plt.hist(df['precipitation'],bins=5)"
   ],
   "id": "e9012297df63bfb4",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
